/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modelo;

import java.io.DataInputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Gauge;

/**
 *
 * @author Guille
 */
public class Conexion {

    private Proxy proxy;
    private Vector descargas;
    private Descarga d;
    private Gauge progress;


    public Conexion()
    {
        descargas = new Vector();
    }

    public void setGauge(Gauge g)
    {
        progress = g;
    }

    public void crearDescarga(String url) throws Exception
    {
        d = new Descarga(url);
        HttpConnection hc = null;
        DataInputStream in = null;
        
        hc = (HttpConnection) Connector.open(d.getUrl());
        hc.setRequestMethod(HttpConnection.HEAD);
        hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
         
        if (hc.getResponseCode() == HttpConnection.HTTP_OK)
        {
            in = hc.openDataInputStream();
            String cad[] = split(url);
            d.setNombre(cad[cad.length-1]);
            d.setTamanio((long) hc.getLength());  
        }
        else
        {
            throw new Exception("Couldn't download");    
        }
    }

    public void iniciarDescargar(String ruta) throws Exception
    {
        System.out.println("Ruta: "+ruta+d.getNombre());
        HttpConnection hc = null;
        DataInputStream in = null;

        hc = (HttpConnection) Connector.open(d.getUrl());
        hc.setRequestMethod(HttpConnection.GET);
        hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
        
        long percent = d.getTamanio() / 100;

        if (hc.getResponseCode() == HttpConnection.HTTP_OK)
        {
            in = hc.openDataInputStream();
            int tamArchivo = (int) hc.getLength();
            
            if (tamArchivo > 0)
            {
                int c = 0;
                String info = "";
                long i = 0;
                int cont = 0;
                while((c=in.read())!=-1)
                {
                    info = info+(char)c;
                    i++;
                    if(i % percent == 0){
                        cont++;
                        progress.setValue(cont);
                        System.out.println(cont);
                    }
                }
                
                System.out.print(info);
            }

          }

          if (in != null) in.close();
          if (hc != null) hc.close();

          descargas.addElement(d);
          
    }

    private String[] split(String original) {

        Vector nodes = new Vector();
        String separator = "/";

        int index = original.indexOf(separator);

        while(index>=0) {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }

        nodes.addElement( original );

        String[] result = new String[ nodes.size() ];
        if( nodes.size()>0 ){
            for(int loop=0; loop<nodes.size(); loop++)
            {
                result[loop] = (String)nodes.elementAt(loop);
            }
        }

        return result;
     }

}
